package Utils;

import org.apache.commons.dbcp2.BasicDataSource;

import java.io.IOException;
import java.sql.Connection;
import java.util.Properties;

public class DBCPjdbc {
    private DBCPjdbc() {
    }

    private static BasicDataSource bs;
    static {
        Properties properties = new Properties();
        try {
            properties.load(DBCPjdbc.class.getClassLoader().getResourceAsStream("mysql.properties"));
            bs = new BasicDataSource();
            bs.setDriverClassName(properties.getProperty("DRIVER"));
            bs.setUrl(properties.getProperty("URL"));
            bs.setUsername(properties.getProperty("USER"));
            bs.setPassword(properties.getProperty("PASSWORD"));
            bs.setInitialSize(10);
            bs.setMaxTotal(12);
            bs.setMaxIdle(4);
            bs.setMinIdle(2);
            bs.setMaxWaitMillis(10000);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = bs.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
    public static BasicDataSource getBs() {
        return bs;
    }

}
